using System;
using System.Collections.Generic;
using UnityEngine;

public class DeckMono : MonoBehaviour
{
    public List<Card> Cards = new List<Card>();
    //public List<CardData> PrefabCardDatas = new List<CardData>();

    public List<Pawn> PrefabPawnDatas = new List<Pawn>();

    /// <summary>
    /// 三选一时直接往这里面加
    /// 新建卡牌在PawnBuilder里
    /// </summary>
    /// <typeparam name="CardPosition"></typeparam>
    /// <returns></returns>
    public List<CardPosition> CardPositions = new List<CardPosition>();

    public Pawn HeroPrefab;

    public void InitDeckByPrefabDatas()
    {
        /* foreach (var data in PrefabCardDatas)
        {
            if (GetOneCardPosition(out CardPosition position))
            {
                var card = PawnBuilder.InitCardByCardData(data);
                card.CurrentPawn.PawnAnchor.SetAnchor(position);
            }
        } */
        foreach (var data in PrefabPawnDatas)
        {
            if (GetOneCardPosition(out CardPosition position))
            {
                var pawn = PawnBuilder.InitPawnByPrefab(data);
                pawn.campIsSelf = true;
                pawn.PawnAnchor.SetAnchor(position);
            }
        }

        if (GameManager.instance.ifHeroMode)
        {
            var hero = PawnBuilder.InitPawnByPrefab(HeroPrefab);
            hero.campIsSelf = true;
            var heroPoint = GridManager.instance.GetGridNode(1, 1);
            hero.gridNode = heroPoint;
            heroPoint.Pawn = hero;
            hero.PawnAnchor.SetAnchor(heroPoint.GetComponent<CardPosition>());
        }
    }

    /// <summary>
    /// 三选一获得新卡牌调用这个
    /// </summary>
    /// <param name="data"></param>
    public bool AddNewCard(Pawn data)
    {
        if (GetOneCardPosition(out CardPosition position))
        {
            var pawn = PawnBuilder.InitPawnByPrefab(data);
            pawn.campIsSelf = true;
            pawn.PawnAnchor.SetAnchor(position);
            return true;
        }
        return false;
    }

    public bool GetOneCardPosition(out CardPosition avalilablePos)
    {
        foreach (var position in CardPositions)
        {
            if (position.IfAvailable)
            {
                avalilablePos = position;
                return true;
            }
        }
        avalilablePos = null;
        return false;
    }

}
